package address

import (
	"awesomeProject/userop-web/api"
	"awesomeProject/userop-web/forms"
	"awesomeProject/userop-web/global"
	"awesomeProject/userop-web/models"
	"awesomeProject/userop-web/proto"
	"context"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
)

func List(ctx *gin.Context) {

	request := proto.AddressRequest{}
	claims, _ := ctx.Get("claims")
	currentUser := claims.(*models.CustomClaims)

	if currentUser.AuthorityId != 2 {
		userId, _ := ctx.Get("userId")
		request.UserId = int32(userId.(uint))
	}

	rsp, err := global.AddressClient.GetAddressList(context.Background(), &request)
	if err != nil {
		zap.S().Info("获取地址列表失败")
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	rspMap := make(map[string]interface{})
	rspMap["total"] = rsp.Total

	addressList := make([]interface{}, 0)
	for _, data := range rsp.Data {
		info := make(map[string]interface{})
		info["id"] = data.Id
		info["user_id"] = data.UserId
		info["province"] = data.Province
		info["city"] = data.City
		info["district"] = data.District
		info["address"] = data.Address
		info["signer_name"] = data.SignerName
		info["signer_mobile"] = data.SignerMobile

		addressList = append(addressList, info)
	}

	rspMap["data"] = addressList
	ctx.JSON(http.StatusOK, rspMap)
}

func Delete(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}
	userId, _ := ctx.Get("userId")
	_, err = global.AddressClient.DeleteAddress(context.Background(), &proto.AddressRequest{
		Id:     int32(id),
		UserId: int32(userId.(uint)),
	})
	if err != nil {
		zap.S().Info("删除地址失败")
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": "删除成功",
	})
}

func Update(ctx *gin.Context) {
	addressForm := forms.AddressForm{}
	if err := ctx.ShouldBind(&addressForm); err != nil {
		zap.S().Info("解析表单失败")
		api.HandleValidateError(err, ctx)
		return
	}
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}
	userId, _ := ctx.Get("userId")

	request := proto.AddressRequest{
		UserId:       int32(userId.(uint)),
		Id:           int32(id),
		Province:     addressForm.Province,
		City:         addressForm.City,
		District:     addressForm.District,
		Address:      addressForm.Address,
		SignerMobile: addressForm.SignerMobile,
		SignerName:   addressForm.SignerName,
	}

	_, err = global.AddressClient.UpdateAddress(context.Background(), &request)
	if err != nil {
		zap.S().Info("更新地址失败")
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": "更新成功",
	})
}

func New(ctx *gin.Context) {

	addressForm := forms.AddressForm{}
	if err := ctx.ShouldBind(&addressForm); err != nil {
		zap.S().Info("解析表单失败")
		api.HandleValidateError(err, ctx)
		return
	}
	userId, _ := ctx.Get("userId")
	request := proto.AddressRequest{
		UserId:       int32(userId.(uint)),
		Province:     addressForm.Province,
		City:         addressForm.City,
		District:     addressForm.District,
		Address:      addressForm.Address,
		SignerMobile: addressForm.SignerMobile,
		SignerName:   addressForm.SignerName,
	}

	rsp, err := global.AddressClient.CreateAddress(context.Background(), &request)
	if err != nil {
		zap.S().Info("创建地址失败")
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"id": rsp.Id,
	})

}
